//引入登录接口
import { LoginReq } from "@/api/user";

const state = {
  token: localStorage.getItem("t_k"), //token
  role: localStorage.getItem("role"), //role
};
const mutations = {
  SET_TOKEN(state, data) {
    //存VUE
    state.token = data;
    //存本地
    localStorage.setItem("t_k", data);
  },
  ROLE(state, data) {
    //存VUE
    state.role = data;
    //存本地
    localStorage.setItem("role", data);
  },
};
const actions = {
  //登录
  A_login({ commit }, data) {
    return new Promise(async (resolve) => {
      //发送请求
      let res = await LoginReq(data);
      //解构
      let { code, token, role } = res.data;
      if (code === 0) {
        commit("SET_TOKEN", "Bearer " + token);
        commit("SET_ROLE", role);
        resolve();
      }
    });
  },
};

export default {
  //命名空间
  namespaced: true,
  state,
  mutations,
  actions,
};
